{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登陆界面</title>
    <link rel="stylesheet" href="{% static 'bootstrap-3.3.7-dist/css/bootstrap.min.css' %}">
</head>
<body>

<div class="container-fluid">
    <div class="row">
        <div class="col-md-4 col-md-offset-4">
            <h3 class="text-center">用户登陆</h3>
            <form class="form-horizontal" id="login_form">
                <div class="form-group">
                    <label for="username" class="col-sm-2 control-label">用户名</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="username" name="username" placeholder="username">
                    </div>
                </div>
                <div class="form-group">
                    <label for="password" class="col-sm-2 control-label">密码</label>
                    <div class="col-sm-10">
                        <input type="password" class="form-control" id="password" name="password"
                               placeholder="password">
                    </div>
                </div>
                <div class="form-group">
                    <label for="verification_code" class="col-md-2 control-label">验证码</label>
                    <div class="col-md-4">
                        <input type="text" name="verification_code" id="verification_code" class="form-control">
                    </div>
                    <div id="verification_code" class="col-md-6">
                        <img src="{% url 'get_verification_img' %}" alt="" id="verifi_img">
                    </div>
                </div>
                {# 用于csrf校验 #}
                {% csrf_token %}
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <input type="button" value="登陆" class="btn btn-primary" id="login_btn">
                        <a href="{% url 'register' %}" class="btn btn-info pull-right">注册</a>
                    </div>
                </div>
                <span id="err_info" class="text-danger"></span>

            </form>
        </div>
    </div>
</div>


<script src="{% static 'js/jquery-3.4.1.js' %}"></script>
<script src="{% static 'js/jquery.cookie.js' %}"></script>
<script src="{% static 'bootstrap-3.3.7-dist/js/bootstrap.min.js' %}"></script>
<script>
    (function () {
        var user = $.cookie("user");
        console.log(user);
        $('#username').val(user);
    })();

    $('#login_btn').click(
        function () {
            var formdata = new FormData($('#login_form')[0]);
            $.ajax({
                url:{% url 'login' %},
                type: "post",
                data: formdata,
                processData: false,
                contentType: false,
                success: function (ret) {
                    if (ret['login_status']) {
                        console.log(ret);
                        $('#err_info').text("登陆成功！");
                        // 如果有登陆后要去的页面，就取到跳转，否则默认以登陆成功跳转的页面
                        if (location.search.slice(6)){
                            location.href = location.search.slice(6);
                        }else{
                            location.href = ret["url"];
                        }
                    } else {
                        if (ret['err_code'] === 10) {
                            $('#err_info').text("用户名或密码错误！");
                        }else if(ret['err_code'] === 11){
                            $('#err_info').text("验证码错误！");
                        }
                    }
                }
            });
        }
    );

    $('#verifi_img').click(
        function () {
            $('#verifi_img')[0].src += '?'
        }
    );

</script>
</body>
</html>